package com.bs.visitservice.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.cache.impl.JedisLoginCache;
import com.bs.visitservice.entity.Complain;
import com.bs.visitservice.service.ComplainService;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import com.bs.visitservice.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 投诉管理,投诉状态 0受理中 1投诉失败 2投诉成功
 */
@RestController
@RequestMapping("/user")
public class ComplainController {

    @Autowired
    private ComplainService complainService;
    @Autowired
    private JedisLoginCache jedisCacheManager;
    /**
     * 新增投诉信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "/saveComplain",method = RequestMethod.POST)
    public Result saveComplain(@RequestBody Map cond){
       Complain complain = (Complain)ResultUtil.transObject(cond, Complain.class);
       Long userNo = (Long) ThreadLocalUtil.get("loginNo");
       complain.setDataTime(new Date());
       complain.setStatus(0);
       complain.setUserNo(userNo);
        boolean isSuc = complainService.save(complain);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }

    /**
     * 查询当前用户投诉信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "/queryComplains",method = RequestMethod.POST)
    public Result queryComplains(@RequestBody Map cond){
        IPage page = ParamUtils.getPage(cond);
        Long userNo = (Long)ThreadLocalUtil.get("loginNo");
        LambdaQueryWrapper<Complain> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Complain::getUserNo,userNo);
        page  = complainService.pageMaps(page,wrapper);
        List<Map<String, Object>> res  = page.getRecords();
        ResultUtil.transfort(res,getDictMap(),true);
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("datas",res);
        return Result.success(result);
    }

    /**
     * 获取列和字典值的映射关系
     * @return
     */
    private Map getDictMap(){
        Map<String,String> dictMap = new HashMap<>();
        dictMap.put("complainType","complainType");
        dictMap.put("status","complainStatus");
        return dictMap;
    }
}
